package com.jz.netthings.application.providerCollection.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jz.netthings.appengine.baseData.JzMachine;
import com.jz.netthings.appengine.baseData.ProviderData;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.appengine.mapper.JzMachineMapper;
import com.jz.netthings.appengine.mapper.ProviderDataMapper;
import com.jz.netthings.appengine.mapper.ProviderInfoMapper;
import com.jz.netthings.appengine.queue.ProviderQueue;
import com.jz.netthings.application.heatseal.bean.HeatSeal;
import com.jz.netthings.application.heatseal.mapper.mysql.HeatSealMapper;
import com.jz.netthings.application.providerCollection.bean.*;
import com.jz.netthings.application.providerCollection.mapper.mysql.CollectionBasemoneyMapper;
import com.jz.netthings.application.providerCollection.mapper.mysql.ProviderCollectionMapper;
import com.jz.netthings.application.providerCollection.service.ProviderCollectionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.netthings.application.providerCost.bean.ProviderCost;
import com.jz.netthings.application.providerCost.mapper.mysql.ProviderCostMapper;
import com.jz.netthings.application.providerReg.bean.ProviderReg;
import com.jz.netthings.application.providerReg.mapper.mysql.ProviderRegMapper;
import com.jz.netthings.application.sample.bean.PlasmaSamples;
import com.jz.netthings.application.sample.mapper.PlasmaSamplesMapper;
import com.jz.netthings.application.stockManage.mapper.ConsumablesStorageMapper;
import com.jz.netthings.bean.system.JyCode;
import com.jz.netthings.bean.system.JyUser;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.security.utils.Token;
import com.jz.netthings.system.mapper.mysql.JyCodeMapper;
import com.jz.netthings.system.mapper.mysql.JyUserMapper;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-03-14
 */
@Service
public class ProviderCollectionServiceImpl extends ServiceImpl<ProviderCollectionMapper, ProviderCollection> implements ProviderCollectionService {
     @Autowired
    JyUserMapper  jyUserMapper;
     @Autowired
     ProviderInfoMapper providerInfoMapper;
     @Autowired
     ProviderCostMapper  providerCostMapper;
     @Autowired
     HeatSealMapper  heatSealMapper;
    @Value("${ly.upload.baseUrl}")
    String  photoUrl;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    JzMachineMapper jzMachineMapper;
    @Autowired
    ProviderRegMapper providerRegMapper;
    @Autowired
    ProviderCollectionMapper  providerCollectionMapper;
    @Autowired
    CollectionBasemoneyMapper collectionBasemoneyMapper;
    @Autowired
    ProviderDataMapper providerDataMapper;
    @Autowired
    PlasmaSamplesMapper plasmaSamplesMapper;

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public Page<ProviderCollection> selectNoDealProviderCollection(Page<ProviderCollection> page, QueryProviderCollection queryProviderCollection) {
        Wrapper<ProviderCollection>    wrapper=new EntityWrapper<>();

        boolean isCurrentDay = true;//是否只查询当天的，如果有时间条件的，则不是查询当天
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartCreateTime(),null)){
            wrapper.ge("createTime",queryProviderCollection.getStartCreateTime());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndCreateTime(),null)){
            wrapper.le("createTime",queryProviderCollection.getEndCreateTime());
            isCurrentDay = false;
        }
        wrapper.eq("isDeal","0");
        wrapper.setSqlSelect("id","idNo","providerNo","cycles","initWeight","totalBloodNum","totalPlasmaNum","useYsNum","useKngNum",
                "startTime","endTime","nurse","machineNo","isDeal","createTime");
        if (isCurrentDay){
            wrapper.like("registerNo",AdscmDateUtils.getNowOnlyDateFomat2(new Date()));
        }
        wrapper.orderBy("createTime",false);
        page = super.selectPage(page, wrapper);
        List<ProviderCollection> providerCollections = page.getRecords();
        for (ProviderCollection providerCollection : providerCollections) {
          if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getProviderNo(),null)){
              ProviderInfo providerInfo = providerInfoMapper.selectById(providerCollection.getProviderNo());
              if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo,null)){
                  providerCollection.setCardId(providerInfo.getCardId());
                  providerCollection.setProviderName(providerInfo.getName());
                  providerCollection.setAge(providerInfo.getAge().toString());
                  providerCollection.setPhone(providerInfo.getPhone());
                  providerCollection.setAddress(providerInfo.getAddress());
                  providerCollection.setPhoto(photoUrl+providerInfo.getPhoto());
                   JyCode  jyCode =new JyCode();
                   jyCode.setType(7);
                   jyCode.setCode(providerInfo.getSex());
                   jyCode= jyCodeMapper.selectOne(jyCode);
                   if (AdscmBaseUtils.getInstance().isNotEmpty(jyCode,null)){
                       providerCollection.setSex(jyCode.getName());
                   }else {
                       providerCollection.setSex(providerInfo.getSex());
                   }
              }
          }
          if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getNurse(),null)){
              JyUser jyUser = jyUserMapper.selectById(providerCollection.getNurse());
              if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                  providerCollection.setNurseName(jyUser.getName());
              }else {
                  providerCollection.setNurseName(providerCollection.getNurse());
              }
          }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getMachineNo(),null)){
                JzMachine jzMachine = jzMachineMapper.selectById(providerCollection.getMachineNo());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jzMachine,null)){
                    providerCollection.setMachineNoName(jzMachine.getName());
                }else {
                    providerCollection.setMachineNoName(providerCollection.getMachineNo());
                }
            }

        }

        page.setTotal(super.selectCount(wrapper));
        return page;
    }
    @Transactional
    @Override
    public Boolean updateNoDealProviderCollection(ProviderCollection providerCollection , Token token) throws AdscmSystemException {
        //先查询到之前的采浆记录，然后根据提交的数据进行修改
        ProviderCollection oldproviderCollection = super.selectById(providerCollection.getId());
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getTotalBloodNum(),null)){
            oldproviderCollection.setTotalBloodNum(providerCollection.getTotalBloodNum());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getTotalPlasmaNum(),null)){
            oldproviderCollection.setTotalPlasmaNum(providerCollection.getTotalPlasmaNum());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getUseKngNum(),null)){
            oldproviderCollection.setUseKngNum(providerCollection.getUseKngNum());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getUseYsNum(),null)){
            oldproviderCollection.setUseYsNum(providerCollection.getUseYsNum());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getInitWeight(),null)){
            oldproviderCollection.setInitWeight(providerCollection.getInitWeight());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getCycles(),null)){
            oldproviderCollection.setCycles(providerCollection.getCycles());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getNurse(),null)){
            oldproviderCollection.setNurse(providerCollection.getNurse());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getStartTime(),null)){
            oldproviderCollection.setStartTime(providerCollection.getStartTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getEndTime(),null)){
            oldproviderCollection.setEndTime(providerCollection.getEndTime());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getIsDeal(),null)){
            oldproviderCollection.setIsDeal(providerCollection.getIsDeal());
            oldproviderCollection.setDealTime(new Date());
            oldproviderCollection.setNurse(token.getUser().getId().toString());
        }

        oldproviderCollection.setModifyTime(new Date());
        oldproviderCollection.setSynDate(AdscmDateUtils.getFormateDate(new Date()));
        boolean result = super.updateById(oldproviderCollection);

        this.dealProviderCollection(oldproviderCollection);

        return result;
    }
    @Transactional
    @Override
    public void dealProviderCollection(ProviderCollection providerCollection) throws AdscmSystemException {

        boolean isSameDay = AdscmDateUtils.isSameDay(new Date(), providerCollection.getCollectionDate());

        if (!isSameDay){
            throw new AdscmSystemException("采浆日期和系统当前日期不是同一天，不能更新费用、热合及第二天的化检记录...");
        }

        logger.info("更新费用记录...");
        //修改费用记录
        //查询费用记录
        Wrapper<ProviderCost> providerCostWrapper =new EntityWrapper<>();
        providerCostWrapper.eq("providerNo",providerCollection.getProviderNo());
        providerCostWrapper.eq("registerNo",providerCollection.getRegisterNo());
        List<ProviderCost> providerCosts = providerCostMapper.selectList(providerCostWrapper);
        if (providerCosts!=null && providerCosts.size()>0){
            ProviderCost providerCost = providerCosts.get(0);
            //根据采浆量计算费用
            Integer totalPlasmaNum = providerCollection.getTotalPlasmaNum();
            String  baseMoney="";
            Wrapper<CollectionBasemoney> collectionBasemoneyWrapper=new EntityWrapper<>();
            collectionBasemoneyWrapper.where(" "+totalPlasmaNum+" <= max AND "+totalPlasmaNum+" > min");
            List<CollectionBasemoney> collectionBasemonies = collectionBasemoneyMapper.selectList(collectionBasemoneyWrapper);
            if (collectionBasemonies!=null&&collectionBasemonies.size()>0){
                CollectionBasemoney collectionBasemoney = collectionBasemonies.get(0);
                baseMoney=collectionBasemoney.getBaseMoney().toString();
            }else {
                baseMoney="0";
            }
            providerCost.setBaseMoney(Float.valueOf(baseMoney));
            providerCost.setCollectionId(providerCollection.getId().toString());
            providerCost.setCollectionDate(providerCollection.getCollectionDate());
            //计算总费用
            float  totalMoney =   providerCost.getBaseMoney() +providerCost.getTrafficFee()+providerCost.getTimesAllowance();
            providerCost.setPutOutMoney(totalMoney);
            providerCost.setModifyTime(new Date());
            providerCost.setSynDate(AdscmDateUtils.getFormateDate(new Date()));
            providerCostMapper.updateById(providerCost);
        }else {
            throw new  AdscmSystemException("没有查询到费用记录");
        }


        //需要采浆量 > 150g  才算是采浆成功，生成热合和化检记录
        if (providerCollection.getTotalPlasmaNum() > 150) {

            //生成热合记录
            logger.info("生成热合记录...");
            Wrapper<HeatSeal> heatSealWrapper = new EntityWrapper<>();
            heatSealWrapper.eq("providerNo", providerCollection.getProviderNo());
            heatSealWrapper.eq("registerNo", providerCollection.getRegisterNo());
            List<HeatSeal> heatSeals = heatSealMapper.selectList(heatSealWrapper);
            if ((heatSeals != null && heatSeals.size() > 0)) {
                HeatSeal heatSeal = heatSeals.get(0);
                heatSeal.setMachineNo(providerCollection.getMachineNo());
                heatSeal.setProviderNo(providerCollection.getProviderNo());
                heatSeal.setPlasmaWeight(providerCollection.getTotalPlasmaNum().toString());
                heatSeal.setPlasmaBagWeiht(providerCollection.getInitWeight().toString());
                heatSeal.setPlasmaBagTotal(String.valueOf((providerCollection.getTotalPlasmaNum() + providerCollection.getInitWeight())));
                heatSeal.setCreateTime(new Date());
                heatSeal.setRegisterNo(providerCollection.getRegisterNo());
                heatSeal.setMODIFY_TIME(AdscmDateUtils.getDateFomat(new Date()));
                heatSeal.setSYNDATE(AdscmDateUtils.getDateFormat(new Date()));
                heatSealMapper.updateById(heatSeal);

            } else {
                //无待热合记录 生成待热合记录
                HeatSeal heatSeal = new HeatSeal();
                heatSeal.setMachineNo(providerCollection.getMachineNo());
                heatSeal.setProviderNo(providerCollection.getProviderNo());
                heatSeal.setPlasmaWeight(providerCollection.getTotalPlasmaNum().toString());
                heatSeal.setPlasmaBagWeiht(providerCollection.getInitWeight().toString());
                heatSeal.setPlasmaBagTotal(String.valueOf((providerCollection.getTotalPlasmaNum() + providerCollection.getInitWeight())));
                heatSeal.setCreateTime(new Date());
                heatSeal.setRegisterNo(providerCollection.getRegisterNo());
                heatSeal.setMODIFY_TIME(AdscmDateUtils.getDateFomat(new Date()));
                heatSeal.setSYNDATE(AdscmDateUtils.getDateFormat(new Date()));
                heatSealMapper.insert(heatSeal);
            }
            //生成明天的小样记录
            //查询缓存
            logger.info("生成第二天的化检记录...");
            ProviderData providerData = providerDataMapper.selectById(Long.valueOf(providerCollection.getProviderNo()));
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerData, null)) {
                if (!providerData.isNeedAssay()) {
                    //查询是否已经存在了小样记录
                    Wrapper<PlasmaSamples> plasmaSamplesWrapper = new EntityWrapper<>();
                    plasmaSamplesWrapper.eq("providerNo", providerData.getProviderId());
                    plasmaSamplesWrapper.eq("registerNo", providerData.getRegisterNo());
                    List<PlasmaSamples> plasmaSamples = plasmaSamplesMapper.selectList(plasmaSamplesWrapper);
                    if (!(plasmaSamples != null && plasmaSamples.size() > 0)) {
                        //没有化检记录
                        //查询最大小样号
                        String sampleNo = "";
                        String day = new SimpleDateFormat("yyyyMMdd").format(new Date());
                        day = String.valueOf(Integer.valueOf(day) + 1);//明天的号
                        Wrapper<PlasmaSamples> getMaxSampleNoWrapper = new EntityWrapper<>();
                        getMaxSampleNoWrapper.like("sampleNo", day, SqlLike.RIGHT);
                        getMaxSampleNoWrapper.orderBy("sampleNo", false);
                        List<PlasmaSamples> getMaxSampleNos = plasmaSamplesMapper.selectList(getMaxSampleNoWrapper);
                        if (getMaxSampleNos != null && getMaxSampleNos.size() > 0) {
                            PlasmaSamples plasmaSample = getMaxSampleNos.get(0);
                            sampleNo = String.valueOf(Long.valueOf(plasmaSample.getSampleNo()) + 1);
                        } else {
                            sampleNo = day + "0001";
                        }
                        PlasmaSamples plasmaSample = new PlasmaSamples();
                        plasmaSample.setProviderNo(String.valueOf(providerData.getProviderId()));
                        plasmaSample.setRegisterNo(providerData.getRegisterNo());
                        plasmaSample.setSampleNo(sampleNo);
                        plasmaSample.setSampleType(0);
                        plasmaSample.setRecodeTime(new Date());
                        plasmaSample.setGatheringPlace(0);
                        plasmaSample.setName(providerData.getName());
                        plasmaSample.setCreateNurse(providerCollection.getNurse());
                        plasmaSample.setGatheringTime(providerCollection.getCollectionDate());
                        plasmaSample.setIdNo(providerData.getIdNo());
                        plasmaSample.setMODIFY_TIME(AdscmDateUtils.getDateFomat(new Date()));
                        plasmaSample.setSYNDATE(AdscmDateUtils.getDateFormat(new Date()));
                        ProviderInfo providerInfo = providerInfoMapper.selectById(providerData.getProviderId());
                        plasmaSample.setBloodType(providerInfo.getBloodType());
                        plasmaSamplesMapper.insert(plasmaSample);
                        //修改登记记录中的小样号
                        Wrapper<ProviderReg> providerRegWrapper = new EntityWrapper<>();
                        providerRegWrapper.eq("providerNo", providerCollection.getProviderNo());
                        providerRegWrapper.eq("registerNo", providerCollection.getRegisterNo());
                        List<ProviderReg> providerRegs = providerRegMapper.selectList(providerRegWrapper);
                        if (providerRegs != null && providerRegs.size() > 0) {
                            ProviderReg providerReg = providerRegs.get(0);
                            providerReg.setSampleNo(plasmaSample.getSampleNo());
                            providerRegMapper.updateById(providerReg);
                        }

                    }
                }
            } else {
                throw new AdscmSystemException("未查询到缓存记录");
            }

        } else {
            logger.info("采浆量不足 150g ...");
        }

    }

    @Override
    public Page<ProviderCollection> selectProviderCollection(Page<ProviderCollection> page, QueryProviderCollection queryProviderCollection) throws AdscmSystemException {
        Wrapper<ProviderCollection>   wrapper =new EntityWrapper<>();

        boolean isCurrentDay = true;//是否只查询当天的，如果有其他条件的，则不是查询当天
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getProviderNo(),null)){
            wrapper.eq("providerNo",queryProviderCollection.getProviderNo());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getNurse(),null)){
            wrapper.eq("nurse",queryProviderCollection.getNurse());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getMachineNo(),null)){
            wrapper.eq("machineNo",queryProviderCollection.getMachineNo());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartTotalBloodNum(),null)){
            wrapper.ge("totalBloodNum",queryProviderCollection.getStartTotalBloodNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndTotalBloodNum(),null)){
            wrapper.le("totalBloodNum",queryProviderCollection.getEndTotalBloodNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartTotalPlasmaNum(),null)){
            wrapper.ge("totalPlasmaNum",queryProviderCollection.getStartTotalPlasmaNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndTotalPlasmaNum(),null)){
            wrapper.le("totalPlasmaNum",queryProviderCollection.getEndTotalPlasmaNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartUseYsNum(),null)){
            wrapper.ge("useYsNum",queryProviderCollection.getStartUseYsNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndUseYsNum(),null)){
            wrapper.le("useYsNum",queryProviderCollection.getEndUseYsNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartUseKngNum(),null)){
            wrapper.ge("useKngNum",queryProviderCollection.getStartUseKngNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndUseKngNum(),null)){
            wrapper.le("useKngNum",queryProviderCollection.getEndUseKngNum());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartCycles(),null)){
            wrapper.ge("cycles",queryProviderCollection.getStartCycles());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndCycles(),null)){
            wrapper.le("cycles",queryProviderCollection.getEndCycles());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartHTC(),null)){
            wrapper.ge("HTC",queryProviderCollection.getStartHTC());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndHTC(),null)){
            wrapper.le("HTC",queryProviderCollection.getEndHTC());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getStartStartTime(),null)){
            wrapper.ge("startTime",queryProviderCollection.getStartStartTime());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getEndStartTime(),null)){
            wrapper.le("startTime",queryProviderCollection.getEndStartTime());
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getSecondPuncture(),null)){
            wrapper.eq("secondPuncture","1");
            isCurrentDay = false;
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryProviderCollection.getRegisterNo(),null)){
            wrapper.like("registerNo",queryProviderCollection.getRegisterNo(),SqlLike.RIGHT);
            isCurrentDay = false;
        }

        if (isCurrentDay){
            wrapper.like("registerNo",AdscmDateUtils.getNowOnlyDateFomat2(new Date()));
        }
        wrapper.eq("isDeal","1");
        wrapper.setSqlSelect("id","idNo","providerNo","cycles","initWeight","totalBloodNum","totalPlasmaNum","useYsNum","useKngNum",
                "startTime","endTime","nurse","machineNo","isDeal","createTime","collTaken","operTime","machineLogId","registerNo","collPhoto");
        wrapper.orderBy("createTime",false);
        RowBounds rowBounds = new RowBounds(queryProviderCollection.getPageSize()*(queryProviderCollection.getCurrentPage()-1)
                ,queryProviderCollection.getPageSize());
        List<ProviderCollection> providerCollections = providerCollectionMapper.selectPage(rowBounds, wrapper);
        for (ProviderCollection providerCollection : providerCollections) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getProviderNo(),null)){
                ProviderInfo providerInfo = providerInfoMapper.selectById(providerCollection.getProviderNo());
                if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo,null)){

                    Wrapper<ProviderReg> regWrapper = new EntityWrapper<>();
                    regWrapper.eq("providerNo",providerCollection.getProviderNo());
                    regWrapper.eq("registerNo",providerCollection.getRegisterNo());
                    List<ProviderReg> regs = providerRegMapper.selectList(regWrapper);
                    if (regs != null && regs.size()>0){
                        providerCollection.setRegPhoto(photoUrl+regs.get(0).getRegPhoto());
                    }

                    providerCollection.setCardId(providerInfo.getCardId());
                    providerCollection.setProviderName(providerInfo.getName());
                    providerCollection.setAge(providerInfo.getAge().toString());
                    providerCollection.setPhone(providerInfo.getPhone());
                    providerCollection.setAddress(providerInfo.getAddress());
                    providerCollection.setPhoto(photoUrl+providerInfo.getPhoto());
                    providerCollection.setIdCardPhoto(photoUrl+providerInfo.getIdCardPhoto());
                    providerCollection.setCollPhoto(photoUrl+providerCollection.getCollPhoto());
                    providerCollection.setInstallStartTime(providerInfo.getIssueCertificateDate());
                    if (AdscmBaseUtils.getInstance().isNotEmpty(providerInfo.getSex(),null)){
                        if ("1".equals(providerInfo.getSex())){
                            providerCollection.setSex("男");
                        }else {
                            providerCollection.setSex("女");
                        }
                    }

                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getCollTaken(),null)){
                String getmin = AdscmDateUtils.getFormatMs(providerCollection.getCollTaken());
                providerCollection.setCollTakenName(getmin);
            }

            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getNurse(),null)){
                JyUser jyUser = jyUserMapper.selectById(providerCollection.getNurse());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    providerCollection.setNurseName(jyUser.getName());
                }else {
                    providerCollection.setNurseName(providerCollection.getNurse());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getMachineNo(),null)){
                JzMachine jzMachine = jzMachineMapper.selectById(providerCollection.getMachineNo());
                if(AdscmBaseUtils.getInstance().isNotEmpty(jzMachine,null)){
                    providerCollection.setMachineNoName(jzMachine.getName());
                }else {
                    providerCollection.setMachineNoName(providerCollection.getMachineNo());
                }
            }
        }
        page.setTotal(super.selectCount(wrapper));
        page.setRecords(providerCollections);
        return page;
    }

    @Override
    public ProviderCollection selectProviderCollectionById(String id) {
        ProviderCollection providerCollection = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getNurse(),null)){
            JyUser jyUser = jyUserMapper.selectById(providerCollection.getNurse());
            if(AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                providerCollection.setNurseName(jyUser.getName());
            }else {
                providerCollection.setNurseName(providerCollection.getNurse());
            }
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getInstallTaken(),null)){
            String formatMs = AdscmDateUtils.getFormatMs(providerCollection.getInstallTaken());
            providerCollection.setInstallTakenName(formatMs);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getPunctureTaken(),null)){
            String formatMs = AdscmDateUtils.getFormatMs(providerCollection.getPunctureTaken());
            providerCollection.setPunctureTakenName(formatMs);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getCollTaken(),null)){
            String formatMs = AdscmDateUtils.getFormatMs(providerCollection.getCollTaken());
            providerCollection.setCollTakenName(formatMs);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(providerCollection.getUninstallTaken(),null)){
            String formatMs = AdscmDateUtils.getFormatMs(providerCollection.getUninstallTaken());
            providerCollection.setUninstallTakenName(formatMs);
        }
        return providerCollection;
    }


    @Value("${jz.unitName}")
    private String unitName;
    @Value("${jz.print.gjzcjjl}")
    private String gjzcjjl;
    
    @Autowired
    ConsumablesStorageMapper consumablesStorageMapper;

    @Override
    public CollPrintSupport selectPrintList(String startDate, String endDate) throws AdscmSystemException {

        logger.info("查询【"+startDate+"--"+endDate+"】的采浆打印数据");
        CollPrintSupport cp = new CollPrintSupport();

        if (AdscmBaseUtils.getInstance().isNotEmpty(startDate,null) &&  AdscmBaseUtils.getInstance().isNotEmpty(endDate,null)){
            /**
             * 报表页眉
             */
            String formHeader = unitName + gjzcjjl;
            String beginTime = startDate +" 00:00:00";
            String endTime = endDate +" 23:59:59";
            List<CollPrintInfo> collPrintInfoList = providerCollectionMapper.selectPrintList(beginTime,endTime);
            /**
             * 总数
             */
            int  total = 0;
            /**
             * 合格数
             */
            int  qualified = 0;
            /**
             * 不合格数
             */
            int  notQualified = 0;

            if (collPrintInfoList != null && collPrintInfoList.size()>0){
                Wrapper<ProviderCollection> wrapper = new EntityWrapper<>();
                wrapper.eq("isDeal",1);
                wrapper.lt("totalPlasmaNum",150);
                wrapper.between("collectionDate",beginTime,endTime);
                notQualified = providerCollectionMapper.selectCount(wrapper);
                total = collPrintInfoList.size();
                qualified = total - notQualified;
            }
            /**
             * 耗材信息
             */
            List<Map<String, String>> hcInfo = cp.getHcInfo();
            List<String> hcCodeList = providerCollectionMapper.selectHcCode(beginTime, endTime);
            if (hcCodeList != null && hcCodeList.size()>0){
                for (String hcCode : hcCodeList) {
                    //根据耗材编号，获取耗材的入库信息
                    String[] hcSplit = null;
                    if (AdscmBaseUtils.getInstance().isNotEmpty(hcCode,null)){
                        hcSplit = hcCode.split("\\|");
                    }
                    if (hcSplit != null && hcSplit.length>0){
                        for (String hc : hcSplit) {
                            List<Map<String, String>> st = consumablesStorageMapper.getStorageByTemplateName(hc);
                            if (st != null && st.size()>0){
                                hcInfo.add(st.get(0));
                            }
                        }
                    }
                }
            }
            cp.setFormHeader(formHeader);
            cp.setCollBeginDate(startDate);
            cp.setCollEndDate(endDate);
            cp.setCollPrintInfoList(collPrintInfoList);
            cp.setTotal(total);
            cp.setQualified(qualified);
            cp.setNotQualified(notQualified);

        }else {
            throw new AdscmSystemException("参数异常，查询采浆打印数据失败...");
        }

        return cp;
    }

}
